package com.rs.game.npc.slayer;

import com.rs.game.Animation;
import com.rs.game.Hit;
import com.rs.game.WorldTile;
import com.rs.game.npc.NPC;
import com.rs.game.player.Player;
import com.rs.game.player.Skills;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.utils.Utils;

@SuppressWarnings("serial")
public class Strykewyrm extends NPC {

	private int stompId;

	public Strykewyrm(int id, WorldTile tile, int mapAreaNameHash, boolean canBeAttackFromOutOfArea, boolean spawned) {
		super(id, tile, mapAreaNameHash, canBeAttackFromOutOfArea, spawned);
		stompId = id;
	}

	@Override
	public void processNPC() {
		super.processNPC();
		if (isDead())
			return;
		if (getId() != stompId && !isCantInteract() && !isUnderCombat()) {
			setNextAnimation(new Animation(12796));
			setCantInteract(true);
			WorldTasksManager.schedule(new WorldTask() {
				@Override
				public void run() {
					if (stompId == 9462) {
						transformIntoNPC(9462);
						setCantInteract(false);
					}
					if (stompId == 9464) {
						transformIntoNPC(9464);
						setCantInteract(false);
					}
					if (stompId == 9466) {
						transformIntoNPC(9466);
						setCantInteract(false);
					}
				}
			});
		}
	}
	
	@Override
	public void handleIngoingHit(final Hit hit) {
		super.handleIngoingHit(hit);
		if (hit.getSource() instanceof Player) {
			Player player = (Player) hit.getSource();
			if (stompId == 9462) {
				if (player.getCombatDefinitions().getSpellId() == 20 || player.getCombatDefinitions().getSpellId() == 21 || player.getCombatDefinitions().getSpellId() == 22 || player.getCombatDefinitions().getSpellId() == 23) {
					hit.setHealHit();
				}
			}
			if (stompId == 9462) {
				if ((player.getCombatDefinitions().getSpellId() == 91 || player.getCombatDefinitions().getSpellId() == 80 || player.getCombatDefinitions().getSpellId() == 63 || player.getCombatDefinitions().getSpellId() == 45) && (player.getEquipment().getCapeId() == 6570 || player.getEquipment().getCapeId() == 23659 || player.getEquipment().getCapeId() == 20771 || player.getEquipment().getCapeId() == 20769)) {
					hit.setDamage((int) (hit.getDamage() * 2));
				}
			}
		}
	}

	@Override
	public void reset() {
		setNPC(stompId);
		super.reset();
	}

	public static void handleStomping(final Player player, final NPC npc) {
		if (npc.isCantInteract())
			return;
		if (!npc.isAtMultiArea() || !player.isAtMultiArea()) {
			if (player.getAttackedBy() != npc && player.getAttackedByDelay() > Utils.currentTimeMillis()) {
				player.getPackets().sendGameMessage("You are already in combat.");
				return;
			}
			if (npc.getAttackedBy() != player && npc.getAttackedByDelay() > Utils.currentTimeMillis()) {
				if (npc.getAttackedBy() instanceof NPC) {
					npc.setAttackedBy(player);
				} else {
					player.getPackets().sendGameMessage("That npc is already in combat.");
					return;
				}
			}
		}
		switch (npc.getId()) {
		case 9462:
			if (player.getSkills().getLevel(Skills.SLAYER) < 93) {
				player.getPackets().sendGameMessage("You need at least a slayer level of 93 to fight this.");
				return;
			}
			if (!player.getTask().getName().equalsIgnoreCase("Ice strykewyrm")) {
				WorldTasksManager.schedule(new WorldTask() {
					int count;
					
					@Override
					public void run() {
						if (count == 0) {
							player.setNextAnimation(new Animation(4278));
							count++;
						} else if (count == 1) {
							player.setNextAnimation(new Animation(857));
							stop();
						}
					}
				}, 1, 1);
				return;
			}
			break;
		case 9464:
			if (player.getSkills().getLevel(Skills.SLAYER) < 77) {
				player.getPackets().sendGameMessage("You need at least a slayer level of 77 to fight this.");
				return;
			}
			if (!player.getTask().getName().equalsIgnoreCase("Desert strykewyrm")) {
				WorldTasksManager.schedule(new WorldTask() {
					int count;
					
					@Override
					public void run() {
						if (count == 0) {
							player.setNextAnimation(new Animation(4278));
							count++;
						} else if (count == 1) {
							player.setNextAnimation(new Animation(857));
							stop();
						}
					}
				}, 1, 1);
				return;
			}
			break;
		case 9466:
			if (player.getSkills().getLevel(Skills.SLAYER) < 73) {
				player.getPackets().sendGameMessage("You need at least a slayer level of 73 to fight this.");
				return;
			}
			if (!player.getTask().getName().equalsIgnoreCase("Jungle strykewyrm")) {
				WorldTasksManager.schedule(new WorldTask() {
					int count;
					
					@Override
					public void run() {
						if (count == 0) {
							player.setNextAnimation(new Animation(4278));
							count++;
						} else if (count == 1) {
							player.setNextAnimation(new Animation(857));
							stop();
						}
					}
				}, 1, 1);
				return;
			}
			break;
		default:
			return;
		}
		WorldTasksManager.schedule(new WorldTask() {

			int count;

			@Override
			public void run() {
				if (count == 0) {
					player.setNextAnimation(new Animation(4278));
					npc.setCantInteract(true);
					count++;
				} else if (count == 1) {
					npc.transformIntoNPC(npc.getId() + 1);
					npc.setNextAnimation(new Animation(12795));
					count++;
				} else if (count == 2) {
					npc.setCantInteract(false);
					npc.setTarget(player);
					npc.setAttackedBy(player);
					stop();
				}
			}

		}, 1, 1);
	}

}
